exports.bless = function(player, req, resp, onHandled) {
    do {
        var user = player.user;
        if( user.status.level < gConfGlobal.PetOpenLevel ) {
            resp.code = 1; resp.desc = 'level not reach'; break;
        }

        var pet = user.pet;
        var petLevel = pet.level;
        
        var now = common.getTime();
        var serverStartDate = new Date(gConfGlobalServer.ServerStartTime*1000);
        var serverStartTime = new Date(serverStartDate.getFullYear(), serverStartDate.getMonth(), serverStartDate.getDate(),0,0,0)/1000;
        var nextPetConf = gConfPet[petLevel+1];
        
        var openDays = 0;
        if( serverStartTime < gConfGlobal.PetOpenRelativeTime ) {
            openDays = nextPetConf.OpenDays;
        }else{
            openDays = nextPetConf.OpenDays2;
        }

        if( !nextPetConf || common.getDayPassed(gConfGlobalServer.ServerStartTime) < openDays ) {
            resp.code = 1; resp.desc = 'level reach max'; break;
        }
        
        var petConf = gConfPet[petLevel];
        var material = player.addInventory('material', petConf.Material, -petConf.BlessCost);
        if (!material) {
            resp.code = 1; resp.desc = 'not enough material'; break;
        }

        // 全民进阶活动
        var activitySchedule = findActivitySchedule('petcost');
        if( activitySchedule ) {
            var confActivities = gConfActivities.petcost; 
            if( user.status.level >= confActivities.OpenLevel ) {
                var petCost = user.activity.pet_cost;
                if( petCost.time != activitySchedule.time ) {
                    petCost.time = activitySchedule.time; 
                    petCost.got = 0;
                    petCost.used = {};
                    player.markDirty('activity.pet_cost');
                } 

                var confId = activitySchedule.conf; 
                if( gConfAvPetCost[confId] && gConfAvPetCost[confId][petLevel+1] && !petCost.got ) {
                    if( !(petLevel in petCost.used) ){
                        petCost.used[petLevel] = 0; 
                    }

                    petCost.used[petLevel] += leftMaterial;
                    player.markDirty(util.format('activity.pet_cost.used.%d',petLevel));
                }
            }
        } 

        // 祝福值过期
        if( pet.bless > 0 && ((pet.bless_timer + petConf.BlessTimeOut * 3600) < now) ) {
            pet.bless_timer = 0;
            pet.bless = 0;
            pet.bless_count = 0;
        }

        pet.bless_count += 1;
        pet.bless += common.randRange(petConf.BlessAddMin, petConf.BlessAddMax);
        var upgrade = false;
        if( pet.bless >= petConf.BlessMax ) {
            upgrade = true;
        }else if( pet.bless_count >= petConf.SuccMax ) {
            upgrade = true;
        }else if( pet.bless_count >= petConf.SuccMin ) {
            var upgradeRate = petConf.UpgradeRate + (pet.bless_count - petConf.SuccMin)*petConf.UpgradeRateAdd;
            if( common.randRange(1, 10000) <= upgradeRate ) {
                upgrade = true;
            }
        }

        if( upgrade ) {
            pet.level += 1;
            pet.bless = 0;
            pet.bless_count = 0;
            pet.bless_timer = 0;
            var newSkill = gConfPet[pet.level].Skill1;
            if( newSkill && pet.skills.indexOf(newSkill) < 0 ) {
                pet.skills.push(newSkill);
                pet.skill_levels[newSkill] = 1;
            }
            
            var newPassiveSkill = gConfPet[pet.level].Skill2;
            if( newPassiveSkill ) {
                pet.skill_levels[newPassiveSkill] = 1;
            }

            player.roleDirty = true;
            if( pet.level > 3 ) {
                gMessage.addSystemMessage('pet_upgrade', [user.info.un, gConfPet[pet.level].Name]);
            }

            player.checkEvent('PetLevel');
        }else{
            if( !pet.bless_timer ) {
                pet.bless_timer = now;
            }
        }
        
        player.markDirty('pet');
        resp.data.pet = pet;
        resp.data.material = material;
        
    }while(false);

    onHandled();
}

exports.save_skill = function(player, req, resp, onHandled) {
    do {
        var skills = req.args.skills;
        if( typeof(skills) != 'object' || !skills.length ) {
            resp.code = 1; resp.desc = 'invalid args'; break;
        }

        var user = player.user;
        var petLevel = user.pet.level;
        var openedSkills = {};
        for( var i=1; i<=petLevel; i++ ) {
            var petConf = gConfPet[i];
            if( petConf.Skill1 ) {
                openedSkills[petConf.Skill1] = 1;
            }
        }

        if( Object.keys(openedSkills).length <= 0 ) {
            resp.code = 1; resp.desc = 'not open skill'; break;
        }

        var validSkill = true;
        var checkdSkills = {};
        for( var i = 0; i<skills.length; i++ ) {
            var skill = skills[i];
            if( isNaN(skill) ) {
                validSkill = false;
                break;
            }

            skill = Math.round(skill);
            if( skill <= 0 || !openedSkills[skill] ) {
                validSkill = false;
                break;
            }

            if( skill in checkdSkills ) {
                validSkill = false;
                break;
            }

            checkdSkills[skill] = 1;
            skills[i] = parseInt(skill);
        }

        if( !validSkill ) {
            resp.code = 1; resp.desc = 'invalid skills'; break;
        }
        
        player.battleWorldSaveTeam = true;
        user.pet.skills = skills;
        player.markDirty('pet.skills');
    }while(false);

    onHandled();
}
